package com.logistics.role.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.account.service.AccountRoleService;
import com.logistics.menu.entity.domain.Menu;
import com.logistics.organization.entity.domain.Organization;
import com.logistics.role.entity.domain.Role;
import com.logistics.role.entity.model.RoleMenuSave;
import com.logistics.role.entity.model.RolePager;
import com.logistics.role.entity.model.RoleRow;
import com.logistics.role.entity.model.RoleSave;
import com.logistics.role.mapper.RoleMapper;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.aspect.annotation.Pager;
import com.logistics.utils.bean.ResultPager;
import com.logistics.utils.constants.Message;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.message.Manager;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService extends ServiceImpl<RoleMapper, Role> {

    @Autowired
    private AccountRoleService accountRoleService;

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 分页查询角色
     * @param param
     * @return
     */
    @Pager
    public ResultPager pager(RolePager param){
        IPage page = new Page(param.getCurrent(), param.getPageSize());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotNull(param.getName())){
            queryWrapper.lambda().like(Role::getName, param.getName());
        }
        IPage<Role> result = this.page(page, queryWrapper.lambda().orderByDesc(Role::getCreateTime));
        return new ResultPager(result.getRecords().stream().map(e -> RoleRow.build(e)).collect(Collectors.toList()),
                (int)result.getTotal());
    }

    /**
     * 查找所有的角色
     * @return
     */
    public List<RoleRow> listAll(){
        return this.list(new LambdaQueryWrapper<Role>()
                .orderByDesc(Role::getCreateTime)).stream()
                .map(e -> RoleRow.build(e)).collect(Collectors.toList());
    }

    /**
     * 查找详细
     * @param id
     * @return
     * @throws MessageException
     */
    public RoleRow details(String id) throws MessageException {
        if(StringUtils.isNull(id)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        RoleRow res = RoleRow.build(this.getOne(new LambdaQueryWrapper<Role>().eq(Role::getId, id)));
        res.setMenuIds(StringUtils.listToString(roleMenuService.menuIdsByRoleId(id), ","));
        return res;
    }

    /**
     * 保存
     * @param save
     */
    @Insert
    @Transactional
    public void save(RoleSave save) throws MessageException {
        if(StringUtils.isNull(save.getMenuIds())){
            throw new MessageException(Manager.getMessage(Message.MSG1009.getCode()));
        }
        if(StringUtils.isNull(save.getId())){
            Role data = Role.build(save);
            this.save(data);
            save.setId(data.getId().toString());
        }else{
            this.update(Role.build(save), new LambdaQueryWrapper<Role>().eq(Role::getId, save.getId()));
        }
        // 先删除
        roleMenuService.deleteByRoleIds(save.getId());
        // 重新添加
        roleMenuService.save(StringUtils.splitToList(save.getMenuIds(), ",")
                .stream().map(e -> RoleMenuSave.build(save.getId(), e)).collect(Collectors.toList()));
    }

    /**
     * 删除
     * @param ids
     */
    public void delete(String ids) throws MessageException {
        if(StringUtils.isNull(ids)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        accountRoleService.checkRoleId(ids);
        // 先删除角色和菜单关联
        roleMenuService.deleteByRoleIds(ids);
        this.remove(new LambdaQueryWrapper<Role>().in(Role::getId, StringUtils.splitToList(ids, ",")));
    }
}
